package com.location.footprint.fragment;

import android.view.Gravity;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.location.footprint.R;
import com.location.footprint.adapter.FragmentStateAdapter;
import com.location.footprint.base.BaseAppAplication;
import com.location.footprint.eventbus.EventCode;
import com.location.footprint.eventbus.EventMessage;
import com.location.footprint.utils.LogUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import butterknife.BindView;

public class FootPrintFragment extends BaseFragment {
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String mTitle;
    private TextView tab_textview;

    public static FootPrintFragment getInstance(String title) {
        FootPrintFragment sf = new FootPrintFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_footprint;
    }

    @Override
    protected void initView() {
        LogUtil.e(mTitle);
        FragmentStateAdapter adapter = new FragmentStateAdapter(getChildFragmentManager());
        adapter.addFragment(HotFootPrintFragment.getInstance("最新"), "最新");
        adapter.addFragment(HotFootPrintFragment.getInstance("热门"), "热门");
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(adapter.getCount());
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(500);
            }
        });
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                onTabSelect();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tablayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        if (event.getCode() == EventCode.EVENT_THEME) {
            BaseAppAplication.theme = (Integer) event.getData();
            onTabSelect();
        }
    }

    private void onTabSelect() {
        TabLayout.Tab tab = tablayout.getTabAt(tablayout.getSelectedTabPosition());
        if (tab.getCustomView() == null) {
            tab_textview = new TextView(getContext());
        }
        tab_textview.setGravity(Gravity.CENTER);
        tab_textview.setTextSize(16);
        tab_textview.setTextColor(ContextCompat.getColor(getContext(),BaseAppAplication.theme));
        tab_textview.setText(tab.getText());
        tab.setCustomView(tab_textview);
        tablayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getContext(), BaseAppAplication.theme));
    }
}